package com.pw.lit.controller;

import ch.qos.logback.core.encoder.EchoEncoder;
import com.pw.lit.bean.*;
import com.pw.lit.bean.Collection;
import com.pw.lit.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/Blog")
public class BlogController {

    @Autowired
    private BlogService blogService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private HistoryService historyService;

    @Autowired
    private CollectionService collectionService;

    @Autowired
    private LabelsService labelsService;

    @Autowired
    private UserService userService;

    @Autowired
    private MessageService messageService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private ReplyService replyService;

    @RequestMapping("/blogindex")
    public String blogindex(HttpSession session,
                            Model model) {
        model.addAttribute("blogs", opened(session, "1"));
        model.addAttribute("cates", getcate(session));
        model.addAttribute("blognum", getcount(session));
        System.out.println("我的博客");
        return "FrontPages/blog/blog-index";
    }

    public List<Category> getcate(HttpSession session) {
        User u = (User) session.getAttribute("user");
        CategoryExample example = new CategoryExample();
        CategoryExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(u.getId());
        return categoryService.selectByExample(example);
    }

    public int getcount(HttpSession session) {
        User u = (User) session.getAttribute("user");
        BlogExample example = new BlogExample();
        BlogExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(u.getId());
        return blogService.countByExample(example);
    }

    //按收藏夹分类
    @RequestMapping("/blogbycate/{cid}")
    @ResponseBody
    public Map<String, Object> blogbycate(@PathVariable("cid") String cid) {
        Map<String, Object> map = new HashMap<>();
        BlogExample example = new BlogExample();
        BlogExample.Criteria criteria = example.createCriteria();
        criteria.andCategoryEqualTo(Integer.parseInt(cid));
        map.put("blogs", blogService.selectByExample(example));
        return map;
    }

    //修改分类名称
    @RequestMapping("/updatecate")
    @ResponseBody
    public Map<String, Object> updatecate(@RequestParam("cid") String cid,
                                          @RequestParam("name") String name) {
        Map<String, Object> map = new HashMap<>();
        Category category = categoryService.selectByPrimaryKey(Integer.parseInt(cid));
        category.setName(name);
        try {
            categoryService.updateByPrimaryKey(category);
            map.put("msg", "修改成功!");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("msg", "修改失败!");
        }
        return map;
    }

    //添加分类
    @RequestMapping("/addcate")
    @ResponseBody
    public Map<String, Object> addcate(HttpSession session,
                                       @RequestParam("name") String name) {
        Map<String, Object> map = new HashMap<>();
        User u = (User) session.getAttribute("user");
        Category category = new Category();
        category.setId(u.getId());
        category.setName(name);
        try {
            categoryService.insert(category);
            map.put("msg", "添加成功!");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("msg", "添加失败!");
        }
        return map;
    }

    @RequestMapping("/opened/{op}")
    @ResponseBody
    public Map<String, Object> blogbyop(HttpSession session,
                                        @PathVariable("op") String op) {
        Map<String, Object> map = new HashMap<>();
        map.put("blogs", opened(session, op));
        return map;
    }

    @RequestMapping("/blogedit/{bid}")
    public String blogedit(Model model,
                           HttpSession session,
                           @PathVariable("bid") String bid) {
        User user = (User) session.getAttribute("user");
        CategoryExample categoryExampleexample = new CategoryExample();
        CategoryExample.Criteria categoryExampleexampleCriteriacriteria = categoryExampleexample.createCriteria();
        categoryExampleexampleCriteriacriteria.andIdEqualTo(user.getId());
        model.addAttribute("cate", categoryService.selectByExample(categoryExampleexample));
        if (bid.equals("0")) {
            model.addAttribute("blog", null);
            return "FrontPages/blog/blog-edit";
        } else {
            LabelsExample example = new LabelsExample();
            LabelsExample.Criteria criteria = example.createCriteria();
            criteria.andBidEqualTo(Integer.parseInt(bid));
            model.addAttribute("labels", labelsService.selectByExample(example));
            model.addAttribute("blog", blogService.selectByPrimaryKey(Integer.parseInt(bid)));
            return "FrontPages/blog/blog-edit";
        }
    }

    //发布博客
    @RequestMapping("/blogrelease")
    @ResponseBody
    public Map<String, Object> blogrelease(HttpSession session,
                                           @RequestParam("title") String title,
                                           @RequestParam("content") String content,
                                           @RequestParam("li") String li,
                                           @RequestParam("col") String col,
                                           @RequestParam("op") String op
    ) {
        Map<String, Object> map = new HashMap<>();
        User user = (User) session.getAttribute("user");
//        Date date = new Date();
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
//        String time=sdf.format(date);
        if (li.length() == 0) {
            map.put("msg", "请至少选择一个标签!");
            return map;
        }
        String[] tip = li.split(",");
        Blog blog = new Blog();
        if (title != "") {
            if (content != "") {
                if (tip.length > 0) {
                    if (col != "") {
                        blog.setTitle(title);
                        blog.setContent(content);
                        blog.setCategory(Integer.parseInt(col));
                        blog.setOpened(op);
                        blog.setId(user.getId());
                        blog.setDate(gettime());
                        blog.setLikeNumber(0);
                        blog.setCollectionNumber(0);
                        blog.setBrowseNumber(0);
                        try {
                            blogService.insert(blog);
                            user.setBlogNumber(user.getBlogNumber() + 1);
                            userService.updateByPrimaryKey(user);
                            for (String la : tip) {
                                Labels labels = new Labels();
                                labels.setBid(blog.getBid());
                                labels.setLabel(la);
                                try {
                                    labelsService.insert(labels);
                                } catch (Exception e) {
                                    map.put("msg", "标签发布失败!");
                                }
                                map.put("msg", "发布成功!");
                            }
                        } catch (Exception e) {
                            map.put("msg", "发布失败!");
                        }
                    } else {
                        map.put("msg", "请至少选择一个标签!");
                    }
                }
            } else {
                map.put("msg", "内容不能为空!");
            }
        } else {
            map.put("msg", "标题不能为空!");
        }
        return map;
    }

    //修改博客
    @RequestMapping("/blogupdate/{bid}")
    @ResponseBody
    public Map<String, Object> blogupdate(HttpSession session,
                                          @RequestParam("title") String title,
                                          @RequestParam("content") String content,
                                          @RequestParam("li") String li,
                                          @RequestParam("col") String col,
                                          @RequestParam("op") String op,
                                          @PathVariable("bid") String bid
    ) {
        Map<String, Object> map = new HashMap<>();
        User user = (User) session.getAttribute("user");
        String[] tip = li.split(",");
        Blog blog = blogService.selectByPrimaryKey(Integer.parseInt(bid));
        //清除旧标签
        if (li.length() != 0) {
            LabelsExample labelsExample = new LabelsExample();
            LabelsExample.Criteria criteria = labelsExample.createCriteria();
            criteria.andBidEqualTo(Integer.parseInt(bid));
            try {
                labelsService.deleteByExample(labelsExample);
                map.put("msg", "标签清空成功!");
            } catch (Exception e) {
                map.put("msg", "标签修改失败!");
            }
        } else {
            map.put("msg", "请至少选择一个标签!");
            return map;
        }
        if (title != "") {
            if (content != "") {
                if (tip.length > 0) {
                    blog.setTitle(title);
                    blog.setContent(content);
                    blog.setCategory(Integer.parseInt(col));
                    blog.setOpened(op);
                    blog.setId(user.getId());
                    blog.setUpdateDate(gettime());
                    try {
                        blogService.updateByPrimaryKey(blog);
                        for (String la : tip) {
                            Labels labels = new Labels();
                            labels.setBid(Integer.parseInt(bid));
                            labels.setLabel(la);
                            try {
                                labelsService.insert(labels);
                            } catch (Exception e) {
                                map.put("msg", "标签发布失败!");
                            }
                            map.put("msg", "修改成功!");
                        }
                    } catch (Exception e) {
                        map.put("msg", "修改失败!");
                    }
                } else {
                    map.put("msg", "请至少选择一个标签!");
                }
            } else {
                map.put("msg", "内容不能为空!");
            }
        } else {
            map.put("msg", "标题不能为空!");
        }
        return map;
    }


    public List<Blog> blogSelectbyid(int id) {
        BlogExample example = new BlogExample();
        BlogExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);
        try {
            return blogService.selectByExample(example);
        } catch (Exception e) {
            return null;
        }
    }

    //获取分页数据
    @RequestMapping("/getblogdata/{id}")
    @ResponseBody
    public <E> List pagedata(Model model,
                             @RequestParam("start") String start,
                             @RequestParam("limit") String limit,
                             @PathVariable("id") String id) {
        List<Blog> blogs = blogSelectbyid(Integer.parseInt(id));
        List<Blog> li = pages(blogs, Integer.parseInt(limit), Integer.parseInt(start) * 7 - 6);
        model.addAttribute("blogs", li);
        model.addAttribute("count", blogs.size());
        return li;
    }

    //分页方法
    public <E> List<E> pages(List<E> list, int limit, int start) {
        List<E> nowList = new ArrayList<E>();
        if (limit + start <= list.size()) {
            for (int i = start - 1; i < start + limit - 1; i++) {
                nowList.add(list.get(i));
            }
        } else {
            for (int i = start - 1; i < list.size(); i++) {
                nowList.add(list.get(i));
            }
        }
        return nowList;
    }

    //公开/私密目录
    public List<Blog> opened(HttpSession session, String op) {
        User user = (User) session.getAttribute("user");
        BlogExample example = new BlogExample();
        BlogExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(user.getId());
        criteria.andOpenedEqualTo(op);
        return blogService.selectByExample(example);
    }

    //设置私密
    @RequestMapping("/changeop/{type}/{bid}")
    @ResponseBody
    public Map<String, Object> changeop(HttpSession session,
                                        @PathVariable("type") String type,
                                        @PathVariable("bid") String bid) {
        Map<String, Object> map = new HashMap<>();
        Blog b = blogService.selectByPrimaryKey(Integer.parseInt(bid));
        if (type.equals("0")) {
            b.setOpened("1");
        } else {
            b.setOpened("0");
        }
        try {
            blogService.updateByPrimaryKey(b);
            map.put("msg", "设置成功!");
        } catch (Exception e) {
            map.put("msg", "设置失败!");
        }
        map.put("blogs", opened(session, type));
        return map;
    }

    //删除博客
    @RequestMapping("/blogdel")
    @ResponseBody
    public Map<String, Object> blogdel(HttpSession session,
                                       Model model,
                                       @RequestParam("bid") String bid) {
        Map<String, Object> map = new HashMap<>();
        try {
            blogService.deleteByPrimaryKey(Integer.parseInt(bid));
            User user = (User) session.getAttribute("user");
            user.setBlogNumber(user.getBlogNumber() - 1);
            userService.updateByPrimaryKey(user);
            map.put("msg", "删除成功!");
        } catch (Exception e) {
            map.put("msg", "删除失败!");
        }
        return map;
    }

    @RequestMapping("/blogdel2/{bid}")
    @ResponseBody
    public Map<String, Object> blogdel2(HttpSession session,
                                        @PathVariable("bid") String bid) {
        Map<String, Object> map = new HashMap<>();
        User user = (User) session.getAttribute("user");
        try {
            blogService.deleteByPrimaryKey(Integer.parseInt(bid));
            user.setBlogNumber(user.getBlogNumber() - 1);
            userService.updateByPrimaryKey(user);
            map.put("msg", "删除成功!");
        } catch (Exception e) {
            map.put("msg", "删除失败!");
        }
        map.put("blogs", opened(session, "1"));
        return map;
    }

    //批量删除
    @RequestMapping("/blogdelall/{li}")
    @ResponseBody
    public Map<String, Object> blogdelall(HttpSession session, @PathVariable("li") String li) {
        Map<String, Object> map = new HashMap<>();
        User user = (User) session.getAttribute("user");
        String[] tip = li.split(",");
        for (String s : tip) {
            try {
                blogService.deleteByPrimaryKey(Integer.parseInt(s));
                user.setBlogNumber(user.getBlogNumber() - 1);
                userService.updateByPrimaryKey(user);
                map.put("msg", "删除成功!");
            } catch (Exception e) {
                map.put("msg", "删除失败!");
            }
        }
        return map;
    }

    //批量设置空开/私密
    @RequestMapping("/blogchangeall/{li}/{ca}")
    @ResponseBody
    public Map<String, Object> blogchangeall(HttpSession session,
                                             @PathVariable("li") String li,
                                             @PathVariable("ca") String ca) {
        Map<String, Object> map = new HashMap<>();
        String[] tip = li.split(",");
        for (String s : tip) {
            try {
                Blog b = blogService.selectByPrimaryKey(Integer.parseInt(s));
                b.setCategory(Integer.parseInt(ca));
                blogService.updateByPrimaryKey(b);
                map.put("msg", "修改成功!");
            } catch (Exception e) {
                map.put("msg", "修改失败!");
            }
        }
        return map;
    }

    //按收藏量排序
    @RequestMapping("/blogbycol/{id}")
    public List<Blog> blogbycol(@PathVariable("id") String id) {
        List<Blog> blogList = blogSelectbyid(Integer.parseInt(id));
        blogList.sort(Comparator.comparing(Blog::getBrowseNumber).reversed());
        return blogList;
    }

    //查看博客
    @RequestMapping("/blogdetail/{bid}")
    public String blogdetai(HttpSession session,
                            Model model,
                            @PathVariable("bid") String bid) {
        User user = (User) session.getAttribute("user");
        Blog blog = blogService.selectByPrimaryKey(Integer.parseInt(bid));
        blog.setBrowseNumber(blog.getBrowseNumber() + 1);

        if (user != null) {
            model.addAttribute("iscoled", iscoled(user.getId(), blog.getBid()));
            History history = new History();
            history.setBid(blog.getBid());
            history.setId(user.getId());
            history.setDate(gettime());

            try {
                blogService.updateByPrimaryKey(blog);
            } catch (Exception e) {
                System.out.println("添加浏览数失败!");
            }
            try {
                historyService.insert(history);
            } catch (Exception e) {
                System.out.println("添加历史记录失败!");
            }
        } else {
            model.addAttribute("iscoled", false);
        }

        //获取blogtip相关内容
        getblogtip(model, blog.getId());

        User user1 = userService.selectByPrimaryKey(blog.getId());
        model.addAttribute("blog", blog);
        model.addAttribute("user1", user1);
        List<Comment> comments = getcomment(bid);
        model.addAttribute("comments", comments);
        model.addAttribute("comuser", getcomuser(comments));
        return "FrontPages/blog/blog-detail";
    }

    //获取blogtip相关
    public Model getblogtip(Model model, int id) {
        List<Blog> blogs = blogSelectbyid(id);
        List<Blog> newblogs = new ArrayList<>();
        for (int i = 0; i < blogs.size(); i++) {
            newblogs.add(blogs.get(blogs.size() - i - 1));
        }
        model.addAttribute("newblogs", newblogs);
        blogs.sort(Comparator.comparing(Blog::getBrowseNumber).reversed());
        List<Blog> hotblogs = new ArrayList<>();
        for (int i = 0; i < blogs.size(); i++) {
            hotblogs.add(blogs.get(i));
        }
        model.addAttribute("hotblogs", hotblogs);

        CommentExample example = new CommentExample();
        CommentExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);
        List<Comment> coms = commentService.selectByExample(example);
        List<Comment> newcoms = new ArrayList<>();
        for (int i = 0; i < coms.size(); i++) {
            newcoms.add(coms.get(coms.size() - i - 1));
        }
        model.addAttribute("newcoms", newcoms);
        return model;
    }

    //获取评论
    public List<Comment> getcomment(String bid) {
        CommentExample example = new CommentExample();
        CommentExample.Criteria criteria = example.createCriteria();
        criteria.andBidEqualTo(Integer.parseInt(bid));
        return commentService.selectByExample(example);
    }

    //获取回复
    @RequestMapping("/getreply/{cid}")
    @ResponseBody
    public Map<String, Object> getreply(@PathVariable("cid") String cid) {
        Map<String, Object> map = new HashMap<>();
        ReplyExample example = new ReplyExample();
        ReplyExample.Criteria criteria = example.createCriteria();
        criteria.andCidEqualTo(Integer.parseInt(cid));
        List<Reply> replyList = replyService.selectByExample(example);
        if (replyList.size() != 0) {
            List<User> replyuser = new ArrayList<>();
            for (Reply r : replyList) {
                replyuser.add(userService.selectByPrimaryKey(r.getId()));
            }
            map.put("replys", replyList);
            map.put("rusers", replyuser);
        }
        return map;
    }

    //获取评论列表的用户列表
    public List<User> getcomuser(List<Comment> list) {
        List<User> userList = new ArrayList<>();
        if (list != null) {
            for (Comment c : list) {
                userList.add(userSelectByUid(c.getIid()));
            }
            return userList;
        } else {
            return null;
        }
    }

    private User userSelectByUid(String uid) {
        UserExample userExample = new UserExample();
        UserExample.Criteria usercriteria = userExample.createCriteria();
        usercriteria.andUidEqualTo(uid);
        List<User> list = userService.selectByExample(userExample);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    //收藏博客
    @RequestMapping("/setcollection/{bid}")
    @ResponseBody
    public Map<String, Object> setcollection(HttpSession session,
                                             @PathVariable("bid") String bid) {
        Map<String, Object> map = new HashMap<>();
        User user = (User) session.getAttribute("user");
        Collection collection = new Collection();
        collection.setId(user.getId());
        collection.setBld(Integer.parseInt(bid));
        collection.setCid(1);

        Blog blog = blogService.selectByPrimaryKey(Integer.parseInt(bid));
        blog.setCollectionNumber(blog.getCollectionNumber() + 1);

        if (user.getId().equals(blog.getId())) {
            map.put("msg", "你不能收藏自己的博客!");
            return map;
        }
        CollectionExample example = new CollectionExample();
        CollectionExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(user.getId());
        criteria.andBldEqualTo(Integer.parseInt(bid));
        if (collectionService.selectByExample(example).size() != 0) {
            map.put("msg", "你已经收藏过该文章了!");
            return map;
        }
        try {
            collectionService.insert(collection);
            sendcoll(blog, user.getUid());
            map.put("msg", "收藏成功!");
        } catch (Exception e) {
            map.put("msg", "收藏失败!");
        }
        try {
            blogService.updateByPrimaryKey(blog);
        } catch (Exception e) {
            map.put("msg", "收藏失败!");
        }
        return map;
    }

    //取消收藏
    @RequestMapping("/delcollection/{bid}")
    @ResponseBody
    public Map<String, Object> delcollection(HttpSession session,
                                             @PathVariable("bid") String bid) {
        Map<String, Object> map = new HashMap<>();
        User user = (User) session.getAttribute("user");
        Blog blog = blogService.selectByPrimaryKey(Integer.parseInt(bid));
        blog.setCollectionNumber(blog.getCollectionNumber() - 1);

        CollectionExample example = new CollectionExample();
        CollectionExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(user.getId());
        criteria.andBldEqualTo(Integer.parseInt(bid));
        try {
            collectionService.deleteByExample(example);
            map.put("msg", "取消收藏成功!");
        } catch (Exception e) {
            map.put("msg", "取消收藏失败!");
        }
        try {
            blogService.updateByPrimaryKey(blog);
        } catch (Exception e) {
            map.put("msg", "取消收藏失败!");
        }
        return map;
    }

    //点赞
    @RequestMapping("/setzan/{bid}")
    @ResponseBody
    public Map<String, Object> setzan(HttpSession session,
                                      @PathVariable("bid") String bid) {
        Map<String, Object> map = new HashMap<>();
        User user = (User) session.getAttribute("user");
        Blog blog = blogService.selectByPrimaryKey(Integer.parseInt(bid));
        blog.setLikeNumber(blog.getLikeNumber() + 1);
        try {
            blogService.updateByPrimaryKey(blog);
            sendzan(blog, user.getUid());
            map.put("msg", "点赞成功!");
        } catch (Exception e) {
            map.put("msg", "点赞失败!");
        }
        return map;
    }

    //评论
    @RequestMapping("/setcom/{bid}")
    @ResponseBody
    public Map<String, Object> setcom(HttpSession session,
                                      @PathVariable("bid") String bid,
                                      @RequestParam("content") String content) {
        Map<String, Object> map = new HashMap<>();
        User user = (User) session.getAttribute("user");
        Blog blog = blogService.selectByPrimaryKey(Integer.parseInt(bid));
        if (content == "" || content == null) {
            map.put("msg", "评论内容不能为空!");
            return map;
        }
        Comment comment = new Comment();
        comment.setId(blog.getId());//博主id
        comment.setBid(blog.getBid());
        comment.setIid(user.getUid());//用户的iid
        comment.setDate(gettime());
        comment.setContent(content);
        comment.setLikeNumber(0);
        try {
            commentService.insert(comment);
            sendcomm(comment, user.getUid());
            map.put("msg", "评论成功!");
        } catch (Exception e) {
            map.put("msg", "评论失败!");
        }
        return map;
    }

    //评论点赞
    @RequestMapping("/comzan/{cid}")
    @ResponseBody
    public int comzan(@PathVariable("cid") String cid) {
        Comment comment = commentService.selectByPrimaryKey(Integer.parseInt(cid));
        comment.setLikeNumber(comment.getLikeNumber() + 1);
        try {
            commentService.updateByPrimaryKey(comment);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return comment.getLikeNumber();
    }

    @RequestMapping("/delcom/{cid}")
    @ResponseBody
    public Map<String, Object> delcom(HttpSession session,
                                      @PathVariable("cid") String cid) {
        Map<String, Object> map = new HashMap<>();
        Comment comment = commentService.selectByPrimaryKey(Integer.parseInt(cid));
        User u = (User) session.getAttribute("user");
        if (u.getUid().equals(comment.getIid())) {
            try {
                commentService.deleteByPrimaryKey(comment.getCid());
                map.put("msg", "删除成功!");
            } catch (Exception e) {
                e.printStackTrace();
                map.put("msg", "删除失败!");
            }
        } else {
            map.put("msg", "你不能删除别人的评论!");
        }
        return map;
    }

    //回复评论
    @RequestMapping("/setreply/{bid}")
    @ResponseBody
    public Map<String, Object> setreply(HttpSession session,
                                        @PathVariable("bid") String cid,
                                        @RequestParam("content") String content) {
        Map<String, Object> map = new HashMap<>();
        User user = (User) session.getAttribute("user");
        if (content == "" || content == null) {
            map.put("msg", "回复内容不能为空!");
            return map;
        }
        Comment comment = commentService.selectByPrimaryKey(Integer.parseInt(cid));
        User user1 = userSelectByUid(comment.getIid());

        Reply reply = new Reply();
        reply.setId(user.getId());
        reply.setCid(Integer.parseInt(cid));
        reply.setIid(user1.getUid());
        reply.setContent(content);
        reply.setDate(gettime());
        reply.setLikeNumber(0);
        try {
            replyService.insert(reply);
            sendrep(reply, user1.getId());
            map.put("msg", "回复成功!");
        } catch (Exception e) {
            map.put("msg", "回复失败!");
        }
        return map;
    }

    //回复回复
    @RequestMapping("/setRreply/{rid}")
    @ResponseBody
    public Map<String, Object> setRreply(HttpSession session,
                                         @PathVariable("rid") String rid,
                                         @RequestParam("content") String content) {
        Map<String, Object> map = new HashMap<>();
        User user = (User) session.getAttribute("user");
        if (content == "" || content == null) {
            map.put("msg", "回复内容不能为空!");
            return map;
        }
        Reply re = replyService.selectByPrimaryKey(Integer.parseInt(rid));
        User user1 = userService.selectByPrimaryKey(re.getId());

        Reply reply = new Reply();
        reply.setId(user.getId());
        reply.setCid(re.getCid());
        reply.setIid(user1.getUid());
        reply.setContent(content);
        reply.setDate(gettime());
        reply.setLikeNumber(0);
        try {
            replyService.insert(reply);
            sendrep(reply, user1.getId());
            map.put("msg", "回复成功!");
        } catch (Exception e) {
            map.put("msg", "回复失败!");
        }
        return map;
    }

    //删除回复
    @RequestMapping("/delreply/{rid}")
    @ResponseBody
    public Map<String, Object> delreply(HttpSession session,
                                        @PathVariable("rid") String rid) {
        Map<String, Object> map = new HashMap<>();
        Reply reply = replyService.selectByPrimaryKey(Integer.parseInt(rid));
        User u = (User) session.getAttribute("user");
        if (u.getId().equals(reply.getId())) {
            try {
                replyService.deleteByPrimaryKey(reply.getRid());
                map.put("msg", "删除成功!");
            } catch (Exception e) {
                e.printStackTrace();
                map.put("msg", "删除失败!");
            }
        } else {
            map.put("msg", "你不能删除别人的回复!");
        }
        return map;
    }

    //回复点赞
    @RequestMapping("/repaddzan/{rid}")
    @ResponseBody
    public int repaddzan(@PathVariable("rid") String rid) {
        Reply reply = replyService.selectByPrimaryKey(Integer.parseInt(rid));
        reply.setLikeNumber(reply.getLikeNumber() + 1);
        try {
            replyService.updateByPrimaryKey(reply);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return reply.getLikeNumber();
    }

    //获取当前时间
    public String gettime() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String time = sdf.format(date);
        return time;
    }

    //判断是否收藏
    public boolean iscoled(int id, int bid) {
        CollectionExample example = new CollectionExample();
        CollectionExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);
        criteria.andBldEqualTo(bid);
        if (collectionService.selectByExample(example).size() != 0) {
            return true;
        } else {
            return false;
        }
    }

    //发送收藏消息
    public void sendcoll(Blog blog, String iid) {
//        Date date = new Date();
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
//        String time=sdf.format(date);

        Message message = new Message();
        message.setId(blog.getId());
        message.setBid(blog.getBid());
        message.setIid(iid);
        message.setDate(gettime());
        try {
            messageService.insert(message);
        } catch (Exception e) {
            System.out.println("发送收藏消息失败!");
        }
    }

    //发送点赞消息
    public void sendzan(Blog blog, String iid) {
//        Date date = new Date();
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
//        String time=sdf.format(date);

        Message message = new Message();
        message.setId(blog.getId());
        message.setBid(blog.getBid());
        message.setIid(iid);
        message.setZid(blog.getBid());
        message.setDate(gettime());
        try {
            messageService.insert(message);
        } catch (Exception e) {
            System.out.println("发送点赞消息失败!");
        }
    }

    //发送评论消息
    public void sendcomm(Comment comment, String iid) {
//        Date date = new Date();
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
//        String time=sdf.format(date);

        Message message = new Message();
        message.setId(comment.getId());
        message.setBid(comment.getBid());
        message.setIid(iid);
        message.setCid(comment.getCid());
        message.setDate(gettime());
        try {
            messageService.insert(message);
        } catch (Exception e) {
            System.out.println("发送评论消息失败!");
        }
    }

    //发送回复消息
    public void sendrep(Reply reply, int iid) {
//        Date date = new Date();
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
//        String time=sdf.format(date);

        Message message = new Message();
        message.setId(iid);
        message.setCid(reply.getCid());
        message.setRid(reply.getRid());
        User u = userService.selectByPrimaryKey(reply.getId());
        message.setIid(u.getUid());
        message.setDate(gettime());
        try {
            messageService.insert(message);
        } catch (Exception e) {
            System.out.println("发送回复消息失败!");
        }
    }
}

